package com.shuziren.controller;

import com.shuziren.core.vo.Result;
import com.shuziren.entity.InterviewPosition;
import com.shuziren.entity.dto.SelectInterviewPositionDto;
import com.shuziren.entity.dto.InterviewPositionDto;
import com.shuziren.entity.dto.UpdateinterviewPositionDto;
import com.shuziren.service.InterviewPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/interviewPosition")
@SuppressWarnings("all")
public class InterviewPositionController {

    @Autowired
    private InterviewPositionService interviewPositionService;

    /**
     * 添加面试职位信息
     * @param interviewPositiondto 面试职位信息
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result addInterviewPosition(@RequestBody  @Validated InterviewPositionDto interviewPositiondto) {
        boolean success = interviewPositionService.addInterviewPosition(interviewPositiondto);
        return success ? Result.success("添加成功") : Result.fail("添加失败");
    }

    /**
     * 删除面试职位信息
     * @param id 面试职位ID
     * @return 操作结果
     */
    @GetMapping("/delete/{id}")
    public Result deleteInterviewPosition(@PathVariable("id") Integer id) {
        boolean success = interviewPositionService.deleteInterviewPosition(id);
        return success ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 更新面试职位信息
     * @param updateinterviewPositionDto 面试职位信息
     * @return 操作结果
     */
    @PostMapping("/update")
    public Result updateInterviewPosition(@RequestBody @Validated UpdateinterviewPositionDto updateinterviewPositionDto) {
        boolean success = interviewPositionService.updateInterviewPosition(updateinterviewPositionDto);
        return success ? Result.success("更新成功") : Result.fail("更新失败");
    }

    /**
     * 根据ID获取面试职位信息
     * @param id 面试职位ID
     * @return 操作结果
     */
    @GetMapping("/get/{id}")
    public Result getInterviewPosition(@PathVariable("id") Integer id) {
        try  {
            if (id <= 0) {
                return Result.fail("ID不能小于等于0");
            }
            InterviewPosition interviewPosition = interviewPositionService.getInterviewPosition(id);
            if (interviewPosition == null){
                return Result.fail("未找到该职位,请重试修改");
            }
            return interviewPosition != null ? Result.success(interviewPosition) : Result.fail("未找到该职位");
        } catch (Exception e) {
            return Result.fail("修改失败,联系管理员:18875694942");
        }
    }

    /**
     * 根据部门、级别、状态和职位名称查询面试职位列表
     * @param queryVO 查询条件
     * @return 面试职位列表
     */
    @PostMapping("/list")
    public Result listInterviewPositions(@RequestBody @Validated SelectInterviewPositionDto queryVO) {
        if (queryVO == null || queryVO.getPageNum() == null || queryVO.getPageSize() == null) {
            return Result.fail("查询条件、页码和每页大小不能为空");
        }
        return Result.success(interviewPositionService.listInterviewPositions(queryVO));
    }

    @GetMapping("/getall")
        public Result getAllInterviewPositions() {
            return Result.success(interviewPositionService.list());
        }
}